package cn.tedu.ivos.inspection.service.impl;

import cn.tedu.ivos.inspection.mapper.InspectionMapper;
import cn.tedu.ivos.inspection.pojo.dto.InspectionQuery;
import cn.tedu.ivos.inspection.pojo.dto.InspectionSaveParam;
import cn.tedu.ivos.inspection.pojo.entity.Inspection;
import cn.tedu.ivos.inspection.pojo.vo.InspectionVO;
import cn.tedu.ivos.inspection.service.InspectionService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class InspectionServiceImpl implements InspectionService {

    @Autowired
    private InspectionMapper inspectionMapper;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public void save(InspectionSaveParam inspectionSaveParam) {
        Inspection inspection = new Inspection();
        BeanUtils.copyProperties(inspectionSaveParam,inspection);
        // 根据车辆id查询车辆信息
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setId(inspectionSaveParam.getVehicleId());
        VehicleVO vehicleVO = vehicleMapper.selectVehicle(vehicleQuery).get(0);
        inspection.setVehicleLicense(vehicleVO.getLicense());
        Vehicle vehicle = new Vehicle();
        vehicle.setId(inspectionSaveParam.getVehicleId());
        if("0".equals(inspectionSaveParam.getStatus())){// 0表示已完成
            vehicle.setStatus("1");// 更新车辆状态 1表示空闲
        }else if("1".equals(inspectionSaveParam.getStatus())){// 1表示送检中
            vehicle.setStatus("4");// 更新车辆状态 4表示送检中
        }
        vehicleMapper.update(vehicle);
        if(inspectionSaveParam.getId()==null){
            inspection.setCreateTime(new Date());
            inspectionMapper.insert(inspection);
        }else{
            inspection.setUpdateTime(new Date());
            inspectionMapper.update(inspection);
        }
    }

    @Override
    public List<InspectionVO> select (InspectionQuery inspectionQuery) {
        List<InspectionVO> inspectionVOList = inspectionMapper.select(inspectionQuery);
        return inspectionVOList;
    }

    @Override
    public void delete(Long id) {
        inspectionMapper.delete(id);
    }
}
